package cn.wolfcode.p2p.website.controller;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.ISystemDictionaryItemService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.bussness.domain.BidRequest;
import cn.wolfcode.p2p.bussness.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

//借款控制器
@Controller
public class BidRequestController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IRealAuthService realAuthService;

    @RequestMapping("/borrowInfo")
    public String basicInfo(Model model) {
        UserInfo userInfo = userInfoService.getCurrent();
        if (!userInfo.isCanBorrow()) {
            return "redirect:/borrow";
        }
        //判断是否有借款正在审核
        if (userInfo.isHasBidRequestApplyProcess()) {
            return "borrow_apply_result";
        }
        model.addAttribute("minBidRequestAmount", Constants.BORROW_AMOUNT_MIN);
        model.addAttribute("account", accountService.getCurrent());
        model.addAttribute("minBidAmount", Constants.BID_AMOUNT_MIN);
        return "/borrow_apply";
    }

    //提交申请按钮
    @RequestMapping("/borrow_apply")
    @ResponseBody
    public AjaxResult borrowApply(BidRequest bidRequest) {
        AjaxResult result = null;
        try {
            bidRequestService.apply(bidRequest);
            result = new AjaxResult();
        } catch (DisplayableExcetion e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, "系统出错,请联系管理员");
        }
        return result;
    }

    //查看标的按钮功能
    @RequestMapping("/borrow_info")
    public String borrowInfo(Long id, Model model) {
        //bidRequest    借款人
        BidRequest bidRequest = bidRequestService.get(id);
        model.addAttribute("bidRequest", bidRequest);
        //userInfo     认证信息
        UserInfo userInfo = userInfoService.get(bidRequest.getCreateUser().getId());
        model.addAttribute("userInfo", userInfo);
        //realAuth  借款人信息
        RealAuth realAuth = realAuthService.get(userInfo.getRealAuthId());
        model.addAttribute("realAuth", realAuth);
        //是否已经借款
        if (UserContext.getCount() == null) {
            model.addAttribute("self", false);
        } else {
            //判断当前登录用户是否借款人
            if (UserContext.getCount().getId().equals(bidRequest.getCreateUser().getId())) {
                //是本人
                model.addAttribute("self", true);
            } else {
                model.addAttribute("self", false);
                //account   账户信息
                model.addAttribute("account", accountService.getCurrent());
            }
        }
        return "borrow_info";
    }

    //马上投标功能
    @RequestMapping("/borrow_bid")
    @ResponseBody
    public AjaxResult borrowBid(Long bidRequestId, BigDecimal amount) {
        AjaxResult result = null;
        try {
            bidRequestService.bid(bidRequestId,amount);
            result = new AjaxResult();
        } catch (DisplayableExcetion e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, "系统出错,请联系管理员");
        }
        return result;
    }
}
